/**
 * Created by zhanglihe on 15/10/30.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
], function ($, common, ready, template, Collection, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },
        fetch: function (_args) {
            var _this = this;
            var args = _args || {};
            args.limit = 10;
            args.offset = 0;
            args.type = type;

            this.collection = new Collection({
                container: 'data-list',
                template: 'data-item',
                url: _global.url.api + 'agency_monthly_reports',
                condition: args,
                model: {
                    tag_name: 'tr',
                    event: {},
                    change: function (e) {
                        this.data[$(e.target).attr('name')] = $(e.target).val();
                    }
                },
                callback: function () {
                    loaded();
                    if (!_this.month_list) {
                        _this.month_list = _this.collection.result.month_list;
                        _this.set_year();
                        var all_amount = _this.collection.result.all_amount;
                        $('#show_platform').html('¥ ' + (all_amount.platform_amount ? all_amount.platform_amount : '0.00'));
                        $('#show_cooperation').html('¥ ' + (all_amount.cooperation_amount ? all_amount.cooperation_amount : '0.00'));
                        $('#show_flow').html('¥ ' + (all_amount.flow_amount ? all_amount.flow_amount : '0.00'));
                        $('#show_pool').html('¥ ' + (all_amount.pool_amount ? all_amount.pool_amount : '0.00'));
                        $('#show_platform_dec').html('¥ ' + (all_amount.platform_dec_amount ? all_amount.platform_dec_amount : '0.00'));
                        $('#show_platform_money').html('¥ ' + (all_amount.platform_money_amount ? all_amount.platform_money_amount : '0.00'));
                        $('#show_update').html(all_amount.update_at ? all_amount.update_at : '-------');
                    }
                    //$('.pagination').prepend('<li><a href="javascript:">结果合计：￥' + _this.collection.result._amount + '</a></li>');
                    //$('.pagination').prepend('<li><a href="javascript:">本页合计：￥' + _this.collection.result._page_amount + '</a></li>');
                }
            });
            this.collection.fetch();
        },
        set_year: function () {
            var _this = this;
            var list = _this.month_list ? _this.month_list : [];
            for (var n in list) {
                if (!isNaN(n)) {
                    $('select[name="year"]').append('<option value="' + n + '">' + n + '</option>');
                }
            }
        },
        event: function () {
            var _this = this;
            /**
             * 搜索事件
             */
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                var year = $('select[name="year"]').val();
                var month = $('select[name="month"]').val();
                if (year < 1) {
                    Alert({msg: '请选择年', type: 'error'});
                    return false;
                }
                _this.collection.condition.year = year;
                _this.collection.condition.month = month;
                _this.collection.reload();
            });
            //联动
            $('#year').on('change', function () {
                $('select[name="month"]').find('option').eq(0).nextAll().remove();
                var id = $(this).val();
                var list = _this.month_list[id] ? _this.month_list[id] : [];
                for (var n = 0; n < list.length; n++) {
                    $('select[name="month"]').append('<option value="' + list[n] + '">' + list[n] + '</option>');
                }
            });
        },

        change: function (e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }

    };

    new index();

});